<template>
  <div class="carDetail">
    <el-row>
      <el-col :span="24"
              class="carDetail-top">
        <div class="carTitle">车辆信息</div>
      </el-col>
      <el-col :span="24">
        <el-form :model="ruleForm"
                 ref="ruleForm"
                 :rules="rules"
                 label-width="100px"
                 class="demo-ruleForm normal-ruleForm">
          <el-form-item label="车牌号"
                        required
                        prop="carNum">
            <el-input class="normal-input"
                      v-model="ruleForm.carNum"></el-input>
          </el-form-item>
          <el-form-item label="车辆类型"
                        required
                        prop="carStyle">
            <el-select v-model="ruleForm.carStyle"
                       class="normal-select"
                       placeholder="请选择车辆类型">
              <el-option v-for="item in options"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="司机姓名"
                        required
                        prop="driver">
            <el-input class="normal-input"
                      v-model="ruleForm.driver"></el-input>
          </el-form-item>
          <el-form-item label="联系电话"
                        required
                        prop="phone">
            <el-input class="normal-input"
                      v-model="ruleForm.phone"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="goBack()">取消</el-button>
            <el-button @click="sureAdd('ruleForm')"
                       type="success">确定添加</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  data () {
    var checkCarNum = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入车牌号'));
      } else if ((/^[\s　]|[ ]$/gi).test(value)) {
        callback(new Error('请输入正确的车牌号'));
      } else {
        callback();
      }
    };
    var checkDriver = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入司机姓名'));
      } else if ((/^[\s　]|[ ]$/gi).test(value)) {
        callback(new Error('请输入正确的司机姓名'));
      } else {
        callback();
      }
    };
    var checkPhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入联系电话'));
      } else if ((/^[\s　]|[ ]$/gi).test(value)) {
        callback(new Error('请输入正确的联系电话'));
      } else {
        callback();
      }
    };
    var checkCarStyle = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请选择车辆类型'));
      } else {
        callback();
      }
    };
    return {
      options: [],
      ruleForm: {
        carNum: "",
        carStyle: "",
        driver: "",
        phone: ""
      },
      rules: {
        carNum: [
          { required: true, validator: checkCarNum, trigger: 'blur' }
        ],
        driver: [
          { required: true, validator: checkDriver, trigger: 'blur' }
        ],
        carStyle: [
          { required: true, validator: checkCarStyle, trigger: 'change' }
        ],
        phone: [
          { required: true, validator: checkPhone, trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    goBack () {
      this.$router.go(-1)
    },
    sureAdd (ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>
<style lang="scss" scoped>
.carDetail {
  padding: 20px;
  .carDetail-top {
    margin-bottom: 20px;
    .carTitle {
      color: #1441b3;
      font-size: 18px;
    }
  }
  .normal-ruleForm {
    .normal-input {
      width: 80%;
    }
    .normal-select {
      width: 80%;
    }
  }
}
</style>
